package com.itheima.health.service.impl;


import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.DemoDate;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 根据月份查询查询对应设置预约数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) throws ParseException {
        //String date =year+"-"+month;
        String be1 = date + "-1";
        String end1 = date + "-31";
        //调用Dao来根据日期查询预约设置信息
//        String转Date
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = simpleDateFormat.parse(be1);
        Date end = simpleDateFormat.parse(end1);

        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(begin, end);
        List<Map> result = new ArrayList<>();
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting orderSetting : orderSettings) {
                Map<String, Object> m = new HashMap<>();
//                获取第几天
                Date time = orderSetting.getOrderDate();
//                Date转String
                SimpleDateFormat s = new SimpleDateFormat();
                s.applyPattern("dd");
                String day = s.format(time);

                m.put("date", day);//获取日期天数字
                m.put("number", orderSetting.getNumber());//获取可预约人数
                m.put("reservations", orderSetting.getReservations());//获取已预约人数
                result.add(m);
            }
        }
        return result;
    }

    /**
     * 根据日期修改预约设置
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //根据日期来查询是否进行了预约
        Date orderDate = orderSetting.getOrderDate();
        Long count =orderSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
//            当前日期已经进行了设置，需要执行更新操作
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
//            当前日期没有预约设置，需要执行插入操作
            orderSettingDao.add(orderSetting);
        }
    }
    @Override
    public void upload(MultipartFile excelFile) {
        try {
            InputStream inputStream = excelFile.getInputStream();
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);

            // 获取sheet页
            XSSFSheet cls412 = excel.getSheetAt(0);
            String sheetName = cls412.getSheetName();
            System.out.println(sheetName);
            // 获取sheet页的最后一行
            int lastRowNum = cls412.getLastRowNum();

            for (int i = 1; i <= lastRowNum; i++) {
                // 获取第i行
                XSSFRow row = cls412.getRow(i);
                // 获取单元格 日期
                XSSFCell cell1 = row.getCell(0);

                // 获取单元格的值
//                System.out.println(cell1.getStringCellValue());
                // 获取单元格 城市
                XSSFCell cell2 = row.getCell(1);
                // 获取单元格的值
                //System.out.println(cell2.getStringCellValue());
                DemoDate date = new DemoDate();
                date.setDate(cell1.getDateCellValue());
                date.setNumber(cell2.getRawValue().toString());

                if (date.getDate().after(new Date())) {
                    orderSettingDao.insertorderSetting(date);
                    log.info("加入数据:时间{},人数{}", date.getDate(), date.getNumber());
                }
            }
            // 数据的导入
            // ArrayList<Object> objects = new ArrayList<>();
            // objects.add(cell1);
            // // mapper
            // this.insertBatch(objects);
            excel.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
